package com.karolak.stock.strategy;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.karolak.stock.data.DataManager;
import com.karolak.stock.indicator.Indicator;

public class Strategy {

	private Config config;

	private DataManager dataManager;

	private Voter voter;
	private Collector collector;

	public Strategy() {
		String chVoter = true ? "v" : null;
		String chCollector = true ? "c" : null;
		
		String json = "{period:15,periodMultipliers:[1,5,25],fftPeriod:512,signalThreshold:0.0003,votingThreshold:0.75,historySize:32000,chartSize:10240}";
		this.config = new Gson().fromJson(json, Config.class);

		updateConfig();
		this.dataManager = new DataManager(getConfig().getHistorySize());

		List<Indicator> voters = defineVoters();

		this.voter = new Voter("v", getDataManager(), -1, getConfig().getSignalThreshold(), getConfig().getVotingThreshold(), voters.toArray(new Indicator[] {}));
		this.collector = new Collector("c", getDataManager(), -1, getVoter());

		getDataManager().addSeries(getVoter(), chVoter);
		getDataManager().addSeries(getCollector(), chCollector);
	}

	protected String defineConfig() {
		return "{}";
	}

	protected void updateConfig() {
		String json = defineConfig();
		Config c = new Gson().fromJson(json, Config.class);

		if (c.getPeriod() > 0) {
			getConfig().setPeriod(c.getPeriod());
		}
		if (c.getPeriodMultipliers() != null) {
			getConfig().setPeriodMultipliers(c.getPeriodMultipliers());
		}
		if (c.getFftPeriod() > 0) {
			getConfig().setFftPeriod(c.getFftPeriod());
		}
		if (c.getSignalThreshold() > 0) {
			getConfig().setSignalThreshold(c.getSignalThreshold());
		}
		if (c.getVotingThreshold() > 0) {
			getConfig().setVotingThreshold(c.getVotingThreshold());
		}
		if (c.getHistorySize() > 0) {
			getConfig().setHistorySize(c.getHistorySize());
		}
		
		if (c.getChartSize() > 0) {
			getConfig().setChartSize(c.getChartSize());
		}		
	}

	protected List<Indicator> defineVoters() {
		List<Indicator> voters = new ArrayList<Indicator>();
		return voters;
	}

	// === get/set === //
	public DataManager getDataManager() {
		return dataManager;
	}

	protected void setDataManager(DataManager dataManager) {
		this.dataManager = dataManager;
	}

	protected Voter getVoter() {
		return voter;
	}

	protected void setVoter(Voter voter) {
		this.voter = voter;
	}

	protected Collector getCollector() {
		return collector;
	}

	protected void setCollector(Collector collector) {
		this.collector = collector;
	}

	public Config getConfig() {
		return config;
	}

	protected void setConfig(Config config) {
		this.config = config;
	}
}
